package com.ruoyi.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.Article;
import com.ruoyi.domain.RecoverStation;
import com.ruoyi.mapper.RecoverStationMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ArticleUncheckMapper;
import com.ruoyi.domain.ArticleUncheck;
import com.ruoyi.service.IArticleUncheckService;
import com.ruoyi.common.core.domain.entity.SysDept;

import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * uncheckService业务层处理
 * 
 * @author ya
 * @date 2025-07-12
 */
@Service
public class ArticleUncheckServiceImpl extends ServiceImpl<ArticleUncheckMapper,ArticleUncheck> implements IArticleUncheckService
{
    @Autowired
    private ArticleUncheckMapper articleUncheckMapper;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private RecoverStationMapper recoverStationMapper;

    
    @Override
    public List<RecoverStation> logicDeleteUncheck(Long[] articleIds) {
        //删除数据到回收站
        ArrayList<RecoverStation> recoverStations = new ArrayList<>();
        for (Long articleId : articleIds){
            ArticleUncheck articleUncheck = articleUncheckMapper.selectArticleUncheckByArticleId(articleId);
            //赋值回收站数据
            RecoverStation recoverStation = new RecoverStation();
            if (articleUncheck != null){
                recoverStation.setUncheckId(articleId);
                if (articleUncheck.getTitle()!= null)
                    recoverStation.setTitle(articleUncheck.getTitle());
                if (articleUncheck.getArticleOrigin()!= null)
                    recoverStation.setArticleOrigin(articleUncheck.getArticleOrigin());
                if (articleUncheck.getColumnId()!= null)
                    recoverStation.setColumnId(articleUncheck.getColumnId());
                if (articleUncheck.getContent()!= null)
                    recoverStation.setContent(articleUncheck.getContent());
                if (articleUncheck.getAuthor()!= null)
                    recoverStation.setAuthor(articleUncheck.getAuthor());
                if (articleUncheck.getCreateBy()!= null)
                    recoverStation.setCreateBy(articleUncheck.getCreateBy());
                if (articleUncheck.getUpdateBy()!= null)
                    recoverStation.setUpdateBy(articleUncheck.getUpdateBy());
                if (articleUncheck.getUpdateTime()!= null)
                    recoverStation.setUpdateTime(articleUncheck.getUpdateTime());
                if (articleUncheck.getCreateTime()!= null)
                    recoverStation.setCreateTime(articleUncheck.getCreateTime());
                if (articleUncheck.getRemark()!= null)
                    recoverStation.setRemark(articleUncheck.getRemark());
                if (articleUncheck.getCoverPath()!= null)
                    recoverStation.setCoverPath(articleUncheck.getCoverPath());
                if (articleUncheck.getIp()!= null)
                    recoverStation.setIp(articleUncheck.getIp());
                if (articleUncheck.getLittleTitle1()!= null)
                    recoverStation.setLittleTitle1(articleUncheck.getLittleTitle1());
                if (articleUncheck.getLittleTitle2()!= null)
                    recoverStation.setLittleTitle2(articleUncheck.getLittleTitle2());
                // 修复日期格式问题
                if (articleUncheck.getPubdate() != null) {
                    // 如果是 java.util.Date 类型，转换为字符串格式
                    if (articleUncheck.getPubdate() instanceof java.util.Date) {
                        // 使用 SimpleDateFormat 格式化日期
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        recoverStation.setPubdate(sdf.format(articleUncheck.getPubdate()));
                    } else {
                        // 如果已经是字符串，直接赋值
                        recoverStation.setPubdate(String.valueOf(articleUncheck.getPubdate()));
                    }
                }
                if (articleUncheck.getState()!= null)
                    recoverStation.setState(articleUncheck.getState());
                if (articleUncheck.getAppval()!= null)
                    recoverStation.setAppval(articleUncheck.getAppval());
                if (articleUncheck.getAttachment()!= null)
                    recoverStation.setAttachment(articleUncheck.getAttachment());
                // 设置默认有效期为30天
                recoverStation.setValidDate(30);
                recoverStation.setType(1L);//回收类型为uncheck
                recoverStations.add(recoverStation);
                recoverStationMapper.insertRecoverStation(recoverStation);
            }

        }
        return recoverStations;
    }

    @Override
    public List<ArticleUncheck> selectArticleListWithPermission(boolean isAdmin, Long deptId, ArticleUncheck articleUncheck) {
        return articleUncheckMapper.selectArticleListWithPermission(isAdmin, deptId, articleUncheck);
    }

    /**
     * 查询uncheck
     * 
     * @param articleId uncheck主键
     * @return uncheck
     */
    @Override
    public ArticleUncheck selectArticleUncheckByArticleId(Long articleId)
    {
        return articleUncheckMapper.selectArticleUncheckByArticleId(articleId);
    }

    /**
     * 查询uncheck列表
     * 
     * @param articleUncheck uncheck
     * @return uncheck
     */
    @Override
    public List<ArticleUncheck> selectArticleUncheckList(ArticleUncheck articleUncheck)
    {
        return articleUncheckMapper.selectArticleUncheckList(articleUncheck);
    }


    /**
     * 新增uncheck
     *
     * @param articleUncheck uncheck
     * @return 结果
     */
    @Override
    public int insertArticleUncheck(ArticleUncheck articleUncheck)
    {
        articleUncheck.setCreateTime(DateUtils.getNowDate());
        return articleUncheckMapper.insertArticleUncheck(articleUncheck);
    }

    /**
     * 修改uncheck
     *
     * @param articleUncheck uncheck
     * @return 结果
     */
    @Override
    public int updateArticleUncheck(ArticleUncheck articleUncheck)
    {
        articleUncheck.setUpdateTime(DateUtils.getNowDate());
        return articleUncheckMapper.updateArticleUncheck(articleUncheck);
    }

    /**
     * 批量删除uncheck
     * 
     * @param articleIds 需要删除的uncheck主键
     * @return 结果
     */
    @Override
    public int deleteArticleUncheckByArticleIds(Long[] articleIds)
    {
        return articleUncheckMapper.deleteArticleUncheckByArticleIds(articleIds);
    }

    /**
     * 删除uncheck信息
     * 
     * @param articleId uncheck主键
     * @return 结果
     */
    @Override
    public int deleteArticleUncheckByArticleId(Long articleId)
    {
        return articleUncheckMapper.deleteArticleUncheckByArticleId(articleId);
    }
}
